package GUI;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.GridLayout;
import javax.swing.JLabel;
import CommonGui.ColorColumnRenderer;
import CommonGui.MyTableModel;
import DataObjects.ConstantObjects;
import Messages.MessageAddItemToSupp;
import Messages.MessageGetAllItems;
import Messages.MessageGetAllItemsReply;
import Messages.MessageGetAllSupplierItems;
import Messages.MessageGetItemsByCategory;
import Messages.MessageGetItemsByCategoryReply;
import Messages.MessageGetSupplierItemsReply;
import Messages.MessageRemoveItemFromSupplier;
import Messages.MessageSavingReply;
import Messages.MessageShowAllSuppliers;
import Messages.MessageShowAllSuppliersReply;
import Messages.MessageUpdateItemsOfSupplier;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;

public class PfAdd_items_to_supplier extends JPanel implements TableModelListener{
	//public class PfAdd_items_to_supplier extends JPanel{
	private MainMenu mainMenu;
	private String suppId;
	
	private JButton leftButton;
	private JButton rightButton;
	
	private VKTextField priceTxt;
	private VKTextField itemTxt;

	
	private JPanel leftPanel;
	private JPanel rightPanel;
	private JPanel categoryPanel;
	private JPanel tablePanel;
	private JPanel allItemsPanel;
	private JPanel middlePanel;
	private JPanel suppItemsPanel;
	private JPanel  allItemsListPanel;
	private JPanel allItemsPanelSearch;
	private JPanel suppItemsPanelSearch;
	private JPanel suppItemsListPanel;
	private JPanel buttonsPanel;
	
	private JList allItemsList;
	private JList suppItemsList;
	private DefaultListModel allItemsModel;
	private DefaultListModel suppItemsModel;
	
	private JTable tbl;
	private MyTableModel dtm;
	
	private Vector<Vector> updates=new Vector<Vector>();             // for price updates 
	private Vector<Vector> res_items;                                //for all items List
	private Vector<Vector> res_items2;                               //for supplier items List

	
	public PfAdd_items_to_supplier(MainMenu mm, String sid)
	{
		
		super();
		this.mainMenu=mm;
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.suppId=sid;		
		initialize();
	}
	
	
	
	private void initialize()
	{
		add(getLeftPanel());
		add(getRightPanel());
		
	}
	
//====================== panels for initialize	========================================
	
	private JPanel getLeftPanel()            //search by category panel and choose specific item panel 
	{
		leftPanel=new JPanel();
		leftPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u05D4\u05D5\u05E1\u05E4\u05EA/\u05D4\u05E1\u05E8\u05EA \u05DE\u05D5\u05E6\u05E8", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.X_AXIS));
		leftPanel.add(getAllItemsPanel());        //panel with searchPanel(searchTxt+searchLbl) + listPanel(list with all items) 
		leftPanel.add(getMiddlePanel());;         // panel with right and left buttons + textfield for price 
		leftPanel.add(getSuppItemsPanel());
	
		return leftPanel;
	}
	
	
	
	private JPanel getAllItemsPanel()
	{
		allItemsPanel=new JPanel();
		allItemsPanel.setLayout(new BoxLayout(allItemsPanel, BoxLayout.Y_AXIS));
		allItemsPanel.add(getAllItemsPanelSearch());
		allItemsPanel.add(getAllListItemsPanel());		
		return allItemsPanel;

	}
	
	private JPanel getAllListItemsPanel ()
	{
		
		allItemsListPanel=new JPanel();
		allItemsListPanel.setBackground(new Color(255, 250, 250));
		allItemsListPanel.setLayout(new BoxLayout(allItemsListPanel, BoxLayout.X_AXIS));	
		allItemsModel=new DefaultListModel();
		allItemsList=new JList();
		allItemsList.setBackground(new Color(230, 230, 250));
		MessageGetAllItems allitems=new MessageGetAllItems(suppId);
		try {
			mainMenu.ms.client.sendToServer(allitems);
			MessageGetAllItemsReply result = (MessageGetAllItemsReply)mainMenu.ms.client.GetMessage();
			this.res_items=result.getItems();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		for(int i=0;i<res_items.size();i++)
		{
			allItemsModel.addElement((String) res_items.get(i).get(1));  
		}
		

		allItemsList.setModel(allItemsModel);		
		allItemsList.setFont(new Font("Tahoma", Font.PLAIN, 18));
		allItemsList.addListSelectionListener(new ListSelectionListener() 
	 	{
	 	      public void valueChanged(ListSelectionEvent evt) {
	 	    	
	 	        if (evt.getValueIsAdjusting())
	 	        return;
	 	       itemTxt.setText((String)allItemsList.getSelectedValue())	 	;
	 	      }
	 	    });
		JScrollPane listScroller1 = new JScrollPane(allItemsList);
		listScroller1.setPreferredSize(new Dimension(200, 600));
		allItemsListPanel.add(listScroller1);
	
		return allItemsListPanel;
	}
	
	
	private JPanel getAllItemsPanelSearch()
	{
		allItemsPanelSearch=new JPanel();
		allItemsPanelSearch.setLayout(new BoxLayout(allItemsPanelSearch, BoxLayout.X_AXIS));
		VKTextField searchTxt=new VKTextField(mainMenu);
		searchTxt.setHorizontalAlignment(JTextField.RIGHT);
		searchTxt.setColumns(10);
		searchTxt.setMaximumSize( searchTxt.getPreferredSize() );
		KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				VKTextField nameField = (VKTextField)arg0.getSource();
				nameField.setHorizontalAlignment(JTextField.RIGHT);
				String selectedName = (String)nameField.getText();
				filterByName(selectedName);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};		
		searchTxt.addKeyListener(keyListener);	
	   
		JLabel searchLbl=new JLabel("חיפוש לפי שם המוצר :");
		searchLbl.setLabelFor(searchTxt);
		allItemsPanelSearch.add(searchTxt);
		allItemsPanelSearch.add(searchLbl);
		
		return allItemsPanelSearch;
	}
	
	
//filter for all items list
	private void filterByName(String str)
	{
		allItemsModel.clear();
		for(int i=0;i<res_items.size();i++)
		{
			if(((String)res_items.get(i).get(1)).startsWith(str))
			allItemsModel.addElement((String) res_items.get(i).get(1));  
		}
	}
	
	
	
// filter for supplier items list
	private void filterByName2(String str)
	{
		suppItemsModel.clear();
		for(int i=0;i<res_items2.size();i++)
		{
			if(((String)res_items2.get(i).get(1)).startsWith(str))
			suppItemsModel.addElement((String) res_items2.get(i).get(1));  
		}
	}
	
	
	
	
	private JPanel getSuppItemsPanel()
	{
		suppItemsPanel=new JPanel();
		suppItemsPanel.setLayout(new BoxLayout(suppItemsPanel, BoxLayout.Y_AXIS));
		suppItemsPanel.add(getSuppItemsPanelSearch());
		suppItemsPanel.add(getSuppListItemsPanel());		
		return suppItemsPanel;

	}
	
	private JPanel getSuppListItemsPanel ()
	{
		
		suppItemsListPanel=new JPanel();
		suppItemsListPanel.setLayout(new BoxLayout(suppItemsListPanel, BoxLayout.X_AXIS));	
		suppItemsModel=new DefaultListModel();
		MessageGetAllSupplierItems suppitems=new MessageGetAllSupplierItems(suppId);
		try {
			mainMenu.ms.client.sendToServer(suppitems);
			MessageGetSupplierItemsReply result = (MessageGetSupplierItemsReply)mainMenu.ms.client.GetMessage();
			this.res_items2=result.getItems();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		
		int size_vector=res_items2.size();		
		for(int i=0;i<size_vector;i++)
		{
			suppItemsModel.addElement((String) res_items2.get(i).get(1));  
		}
		
		suppItemsList = new JList(suppItemsModel);		
		suppItemsList.setFont(new Font("Tahoma", Font.PLAIN, 18));
		suppItemsList.setBackground(new Color(230, 230, 250));
		JScrollPane listScroller2 = new JScrollPane(suppItemsList);
		listScroller2.setPreferredSize(new Dimension(200, 600));
		suppItemsListPanel.add(listScroller2);
		
		suppItemsList.addListSelectionListener(new ListSelectionListener() 
	 	{
	 	      public void valueChanged(ListSelectionEvent evt1) {
	 	       if (evt1.getValueIsAdjusting())    return;
	 	       itemTxt.setText((String)suppItemsList.getSelectedValue())	 	;	
	 	      }
	 	    });
	
		return suppItemsListPanel;
	}
	
	
	private JPanel getSuppItemsPanelSearch()
	{
		suppItemsPanelSearch=new JPanel();
		suppItemsPanelSearch.setLayout(new BoxLayout(suppItemsPanelSearch, BoxLayout.X_AXIS));
		VKTextField searchTxt=new VKTextField(mainMenu);
		searchTxt.setHorizontalAlignment(JTextField.RIGHT);
		
		KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg0) {}

			public void keyReleased(KeyEvent arg0) 
			{
				VKTextField nameField = (VKTextField)arg0.getSource();
				nameField.setHorizontalAlignment(JTextField.RIGHT);
				String selectedName = (String)nameField.getText();
				filterByName2(selectedName);				
			}

			public void keyTyped(KeyEvent arg0) {}		
		};		
		searchTxt.addKeyListener(keyListener);
		
		JLabel searchLbl=new JLabel("חיפוש לפי שם המוצר :");
		searchLbl.setLabelFor(searchTxt);
		suppItemsPanelSearch.add(searchTxt);
		suppItemsPanelSearch.add(searchLbl);
		
		return suppItemsPanelSearch;
	}
	
	
	
	
	
	
	private JPanel getMiddlePanel()
	{
		middlePanel=new JPanel();
		
		
		GridBagLayout gbl = new GridBagLayout();    
		middlePanel.setLayout(gbl);
		GridBagConstraints gbc1 = new GridBagConstraints();
		GridBagConstraints gbc2 = new GridBagConstraints();
		GridBagConstraints gbc3 = new GridBagConstraints();
		GridBagConstraints gbc4 = new GridBagConstraints();
		GridBagConstraints gbc5 = new GridBagConstraints();
		GridBagConstraints gbc6 = new GridBagConstraints();

		
		gbc1.fill=GridBagConstraints.HORIZONTAL;
		gbc1.gridheight=1;
		gbc1.gridwidth=0;
		gbc1.insets = new Insets(0,0,50,0);
		
		gbc4.fill=GridBagConstraints.HORIZONTAL;
		gbc4.gridheight=1;
		gbc4.gridwidth=0;
		gbc4.insets = new Insets(0,0,50,0);
	
		
		gbc2.gridheight=1;
		gbc2.gridwidth=0;
		gbc2.insets = new Insets(0,0,0,0);
		
		gbc6.gridheight=1;
		gbc6.gridwidth=0;
		gbc6.insets = new Insets(0,0,0,0);
		
		
		gbc3.gridheight=1;
		gbc3.gridwidth=0;
		gbc3.insets = new Insets(0,0,20,0);
		
		gbc5.gridheight=1;
		gbc5.gridwidth=0;
		gbc5.insets = new Insets(0,0,20,0);
		
		middlePanel.add(getRightButton(),gbc1);


		middlePanel.add(getLeftButton(),gbc4);

		JLabel priceLbl=new JLabel("מחיר :");
		middlePanel.add(priceLbl,gbc2);
		middlePanel.add(getPriceTxt(),gbc3);
		JLabel itemLbl=new JLabel("מוצר :");
		middlePanel.add(itemLbl,gbc6);
		middlePanel.add(getItemTxt(),gbc5);	

		return middlePanel;
	}
	
	
	
	
	
	
	
	private JButton getRightButton()
	{
		rightButton=new JButton("--->");
		rightButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				if(priceTxt.getText().isEmpty()==false)
				{
				String selectedItem=(String)allItemsList.getSelectedValue();
				if (selectedItem!=null)
				{
					itemTxt.setText(selectedItem);
					allItemsModel.removeElement(selectedItem);
					suppItemsModel.addElement(selectedItem);
					for(int i=0; i<res_items.size();i++)
					{
					     if(res_items.get(i).get(1).equals(selectedItem))
					     {
					    	 Vector<String> itemToSave=new Vector<String>();
					    	 Vector<String> row=new Vector<String>();
					    	 itemToSave.add((String) res_items.get(i).get(0));           //Item ID
					    	 itemToSave.add(suppId);                                     //supp ID
					    	 itemToSave.add(priceTxt.getText());                         //price
					    	 
					    	 row.add((String) res_items.get(i).get(0));                 //Item ID
					    	 row.add((String) res_items.get(i).get(1));                 //Item Name
					    	 row.add(priceTxt.getText());                               //Item Price
					    	 
					    	 priceTxt.setText(null);
					    	 
					    	 MessageAddItemToSupp addItem= new MessageAddItemToSupp(itemToSave);
					    	 try {
							    	mainMenu.ms.client.sendToServer(addItem);
							    	MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
							    	if (result.getResult()== 0)
							    	{
							    		JOptionPane.showMessageDialog(null,"בעיית תקשורת 01");
							    	}
							    	else
							    	{	
							    		MyTableModel orderModel = (MyTableModel) (tbl
												.getModel());
										orderModel.addRow(row);
										tbl.setModel(orderModel);
										
							    		//res_items2.add(row);
							    		//res_items.remove(i);
								    	tbl.repaint();
								    	break;
							    	}
								
							      } catch (IOException e1) 
							           {
								           e1.printStackTrace();
							           }
					    	 
					    	 
					    
					     }
					}
				} //if (selectedItem!=null)
				
				else
				{
					JOptionPane.showMessageDialog(null,"יש לבחור פריט");
				}
				
				
				}// 	if(priceTxt.getText().isEmpty()==false)
				
				else
				{
					JOptionPane.showMessageDialog(null,"יש להקיש מחיר הפריט");
				}
			}
		});
	
		
		return rightButton;		
	}
	
	private JButton getLeftButton()
	{
		leftButton=new JButton("<---");
		
		leftButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				String selectedItem=(String)suppItemsList.getSelectedValue();
				if (selectedItem!=null)
				{
					suppItemsModel.removeElement(selectedItem);
					allItemsModel.addElement(selectedItem);
					for(int i=0; i<res_items2.size();i++)
					{
					     if(res_items2.get(i).get(1).equals(selectedItem))
					     {
					    	 
					    	 MessageRemoveItemFromSupplier removeItem= new MessageRemoveItemFromSupplier((String)res_items2.get(i).get(0),suppId);
					    	 try {
							    	mainMenu.ms.client.sendToServer(removeItem);
							    	MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
							    	if (result.getResult()== 0)
							    	{
							    		JOptionPane.showMessageDialog(null,"בעיית תקשורת 02");
							    	}
								
							      } catch (IOException e1) 
							          {
								           e1.printStackTrace();
							          }
					    	 
					    	 res_items.add(res_items2.get(i));
					    	 res_items2.remove(i);
					    	 tbl.repaint();
					    	 break;
					     }
					}
				} //if (selectedItem!=null)
				
				else
				{
					JOptionPane.showMessageDialog(null,"יש לבחור פריט");
				}
						
			}
		});
		
		return leftButton;		
	}
	
	
	private VKTextField getPriceTxt()
	{
		priceTxt=new VKTextField(mainMenu);
		priceTxt.setHorizontalAlignment(JTextField.RIGHT);
		priceTxt.setColumns(10);

		
		KeyListener keyListener = new KeyListener() 
		{		      
			public void keyPressed(KeyEvent arg2) {}

			public void keyReleased(KeyEvent arg3) 
			{
				
								
			}

			public void keyTyped(KeyEvent arg1) 
			{
				 char a = arg1.getKeyChar();
		          if (!Character.isDigit(a)
		              && (a != '.')
		              && (a != '\b')) {
		        	  arg1.consume();
		      
			}		
		}};		
		priceTxt.addKeyListener(keyListener);
		return priceTxt;
	}
	
	
	private VKTextField getItemTxt()
	{
		itemTxt=new VKTextField(mainMenu);
		itemTxt.setHorizontalAlignment(JTextField.RIGHT);
		itemTxt.setColumns(10);

		
		return itemTxt;
	}
	
	
	
	private JPanel getRightPanel()
	{
		rightPanel=new JPanel();
		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
		rightPanel.add(getCategoryPanel());            // panel with buttons for choosing item categories
		rightPanel.add(getTablePanel());             // panel with items table
		rightPanel.add(getButtonsPanel());           // panel with save and cancel buttons 
		
		return rightPanel;
	}
	
//=====================panels for leftPanel ============================================
	
	private JPanel getCategoryPanel()
	{
		categoryPanel=new JPanel();
		categoryPanel.setBorder(new TitledBorder(null, "\u05E1\u05D5\u05D2 \u05DE\u05D5\u05E6\u05E8", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		GridLayout grid =new GridLayout(0, 4, 7, 7);
		categoryPanel.setLayout(grid);
		ConstantObjects consobj=new ConstantObjects();
		String [] categories=consobj.getItemCategories();
		for(int i=0; i<categories.length;i++)
		{
			
			categoryPanel.add(getCategoryButton(categories[i]));
		}
		return categoryPanel;
	}

	
	
	private JPanel getTablePanel()
	{
		tablePanel=new JPanel();
		tablePanel.setBorder(new TitledBorder(null, "\u05DE\u05D5\u05E6\u05E8\u05D9 \u05D4\u05E1\u05E4\u05E7", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		Vector <String> tblheader=new Vector<String>();
		tblheader.add("מקט");
		tblheader.add("פריט");
		tblheader.add("מחיר");
		
		dtm=new MyTableModel(res_items2,tblheader);	
		
		tbl = new JTable(dtm);
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		for(int x=0;x<3;x++){
			tbl.getColumnModel().getColumn(x).setCellRenderer( centerRenderer );
	        }
		TableColumn tm = tbl.getColumnModel().getColumn(2);
		
		
	    tm.setCellRenderer(new ColorColumnRenderer(Color.green, Color.MAGENTA));
		tbl.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		tbl.setFont(new Font("David", Font.BOLD, 16));
		tbl.getTableHeader().setFont( new Font( "Arial" , Font.BOLD, 17 ));
		tbl.getTableHeader().setBackground(new Color(173,216,230));
		tbl.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 
	    tbl.setRowHeight(30);
		tbl.setFillsViewportHeight(true);
        tbl.setGridColor(new Color(25, 25, 112));
        tbl.setBackground(new Color(224, 255, 255));
        tbl.setForeground(new Color(0, 0, 0));
        tbl.setAutoCreateRowSorter(true);
        
       // tbl.add
        tbl.getModel().addTableModelListener(this);                                        
        JScrollPane scrollPane = new JScrollPane(tbl);
        scrollPane.setPreferredSize(new Dimension(370, 250));
        //scrollPane.setMinimumSize(new Dimension(370, 100));
       // scrollPane.setMaximumSize(new Dimension(370, 100));
        
        tablePanel.add(scrollPane);
		
		return tablePanel;
	}
	
	private JPanel getButtonsPanel()
	{
		buttonsPanel=new JPanel();
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
		buttonsPanel.add(getSaveButton());
		buttonsPanel.add(Box.createHorizontalStrut(25));
		buttonsPanel.add(getCancelButton());
		return buttonsPanel;
		
	}
	
	
	
	private JButton getSaveButton()
	{
		JButton saveButton=new JButton("שמור");
		
		saveButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {

				if(updates.size()>0)
				{

					MessageUpdateItemsOfSupplier msgUpd= new MessageUpdateItemsOfSupplier(updates);
			    	 try {

					    	mainMenu.ms.client.sendToServer(msgUpd);
					    	MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
					    	if (result.getResult()== 0)
					    	{
					    		JOptionPane.showMessageDialog(null,"בעיית תקשורת 03");
					    	}
					    	
					    	else 
					    	{
					    		JOptionPane.showMessageDialog(null,"הנתונים נשמרו בהצלחה");
					    	}
						
					      } catch (IOException e1) 
					          {
						           e1.printStackTrace();
					          }
				}
				
		
		}});
		
		return saveButton;
	}
	
	private JButton getCancelButton()
	{
		JButton cancelButton=new JButton("ביטול");
		
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				MessageShowAllSuppliers msgShowSupp=new MessageShowAllSuppliers( );
				try {
					mainMenu.ms.client.sendToServer(msgShowSupp);
					MessageShowAllSuppliersReply resultSupp=(MessageShowAllSuppliersReply)mainMenu.ms.client.GetMessage();
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().add(new PfSearch_supplier(mainMenu,resultSupp.getResult()), BorderLayout.CENTER);	
					mainMenu.getContentPane().add(new PanelFunctionsSuppliers(mainMenu), BorderLayout.EAST);
					mainMenu.getContentPane().validate();
				}
				catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
									
			}
		});
		return cancelButton;
	}
	
	
	private JButton getCategoryButton(String name)
	{
		JButton catButt=new JButton(name);
		catButt.setPreferredSize(new Dimension(80,80));
		catButt.setBackground(new Color(0, 139, 139));
		catButt.setForeground(new Color(255, 255, 255));
		catButt.setFont(new Font("Tahoma", Font.BOLD, 15));
		catButt.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent ebutt) 
			{
				JButton butt=(JButton) ebutt.getSource();
				MessageGetItemsByCategory getItems=new MessageGetItemsByCategory(butt.getText(),suppId);
				try {
					mainMenu.ms.client.sendToServer(getItems);	
					MessageGetItemsByCategoryReply result=(MessageGetItemsByCategoryReply)mainMenu.ms.client.GetMessage();
					res_items.removeAllElements();
					allItemsModel.clear();
					suppItemsModel.clear();

					for(int i=0;i<result.getAllItems().size();i++)
					{
						allItemsModel.addElement((String)result.getAllItems().get(i).get(1));  // display item names of chosen category in All Items JList
						res_items.add(result.getAllItems().get(i));
					}
					
					for(int i=0;i<result.getSuppItems().size();i++)
					{
						suppItemsModel.addElement((String) result.getSuppItems().get(i).get(1));  // display item names of chosen category in Supplier Items JList
					}
					
				  tbl.repaint();
				    }	
				catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		return catButt;
	}
	
	

	
//listener for JTable changes
	
	
	public void tableChanged(TableModelEvent e) {
		
		Vector<String> itemUpd=new Vector<String>();
		int row = e.getFirstRow();
        int column = e.getColumn();
        System.out.println("row = "+row+"                column= "+column);
        if(column>=0 && row>=0)
        {
        DefaultTableModel model = (DefaultTableModel)e.getSource();
        String columnName = model.getColumnName(column);      
        String price =(String) model.getValueAt(row, column);
        String itemId=(String) model.getValueAt(row, 0);
      tbl.isCellEditable(1, 1);
        
       
        
        //if the price of the item was already changed , 
        //remove  old price and insert  new price into the Vector of updates
        for(int i=0;i<updates.size();i++)
        {
        	if(updates.get(i).get(0).equals(itemId))
        	{
        		updates.remove(i);
        		break;
        	}
        }
        
        itemUpd.add(itemId);
        itemUpd.add(suppId);
        itemUpd.add(price);
        updates.add(itemUpd);
        }
	}
		
	
}


